package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-05-31
 */
@Service
@Transactional
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    @Resource
    private EduSubjectMapper subjectMapper;

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取课程分类列表（树形）
    @Override
    public List<OneSubject> getAllSubject() {
        //查询所有一级和二级分类 parent_id=0 一级 parent_id！=0 二级
        QueryWrapper<EduSubject> oneWrapper = new QueryWrapper<>();
        oneWrapper.eq("parent_id", "0");
        List<EduSubject> oneSubjectList = subjectMapper.selectList(oneWrapper);

        QueryWrapper<EduSubject> twoWrapper = new QueryWrapper<>();
        twoWrapper.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = subjectMapper.selectList(twoWrapper);

        //创建list集合，用于存储最终封装对象
        List<OneSubject> allSubject = new ArrayList<>();//返回最终数据
        for (EduSubject subject : oneSubjectList) {

            OneSubject oneSubject = new OneSubject();
            oneSubject.setId(subject.getId());
            oneSubject.setTitle(subject.getTitle());
            allSubject.add(oneSubject);

            List<TwoSubject> twoSubjects = new ArrayList<>();
            for (EduSubject subject2 : twoSubjectList) {
                TwoSubject twoSubject = new TwoSubject();
                if (subject2.getParentId().equals(subject.getId())) {
                    twoSubject.setId(subject2.getId());
                    twoSubject.setTitle(subject2.getTitle());
                    twoSubjects.add(twoSubject);
                }
            }
            if (twoSubjects.size() >= 1) {
                oneSubject.setChildren(twoSubjects);
            }
            allSubject.add(oneSubject);
        }

        return allSubject;

    }

    //获取课程分类列表（树形）
//    @Override
//    public List<OneSubject> getAllSubject() {
//        //查询所有一级和二级分类 parent_id=0 一级 parent_id！=0 二级
//        QueryWrapper<EduSubject> oneWrapper=new QueryWrapper<>();
//        QueryWrapper<EduSubject> twoWrapper=new QueryWrapper<>();
//        oneWrapper.eq("parent_id","0");
//        twoWrapper.ne("parent_id","0");
//        List<EduSubject> oneSubjectList=subjectMapper.selectList(oneWrapper);
//        List<EduSubject> twoSuubjectList=subjectMapper.selectList(twoWrapper);
//
//        //创建list集合，用于存储最终封装对象
//        List<OneSubject> allSubject=new ArrayList<>();//返回最终数据
//
//        for (EduSubject subject:oneSubjectList) {
//            OneSubject oneSubject=new OneSubject();//定义单个一级数据
//            oneSubject.setId(subject.getId());
//            oneSubject.setTitle(subject.getTitle());
//
//            List<TwoSubject> twoSubjects=new ArrayList<>();//二级课程集合
//            for (EduSubject subjectTwo:twoSuubjectList){
//                TwoSubject twoSubject=new TwoSubject();//二级课程
//                if(subjectTwo.getParentId().equals(oneSubject.getId())){
//                    twoSubject.setId(subjectTwo.getId());
//                    twoSubject.setTitle(subjectTwo.getTitle());
////                    twoSubjects =oneSubject.getChildren();
//                    twoSubjects.add(twoSubject);
//                }
//            }
//            if(twoSubjects.size()>=1){
//                oneSubject.setChildren(twoSubjects);
//            }
//
//            allSubject.add(oneSubject);
//        }
//
//
//        return allSubject;
//    }
}

